﻿/** 
 * Description:订单基本信息DAO
 * Date:2011-7-16
 * Author:Along	
*/
package com.community.shopping.dao;

import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.Query;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.community.shopping.domain.Order;
import com.community.shopping.service.logic.Page;

public class OrderDao extends HibernateDaoSupport{
	private Log log = LogFactory.getLog(GoodsTypeDao.class);  
	

	public void save(Order order){
		getHibernateTemplate().saveOrUpdate(order);	
		log.debug("-------保存一行order-------");
	}

	public void delete(Order order){		  
		getHibernateTemplate().delete(order);
		log.debug("---------删除一行order-------");
	}


	public List<?> getOrder() {  		
	 return getHibernateTemplate().find(" from Order order by createDate desc"); 
	 }

	public List<?> getOrder(String sql){
		String finalsql="from Order where "+sql;
		return getHibernateTemplate().find(finalsql +" order by createDate desc");
	}   
	
	public String getMaxNum(String today)
	{		
		String hql="select max(o.orderNo) from Order o where o.orderNo like '"+today+"%'";
		return getHibernateTemplate().find(hql).get(0)==null?"":getHibernateTemplate().find(hql).get(0).toString();
	}
	
	
	public Order getOrderById(String id){
		Order order=getHibernateTemplate().get(Order.class, Integer.parseInt(id));
		return order;
	} 
	
	public List<?> getOrder(String sql,Page page){
		String finalsql=" from Order where "+sql;
		Query   query   = this.getSession().createQuery(finalsql+" order by createDate desc"); 
		query.setFirstResult(page.getPageStartRow()>0?page.getPageStartRow()-1:0); 
		query.setMaxResults(page.getPageSize()); 
		if(page.getTotalRowsAmount()==0)
		{
		page.setTotalRowsAmount(getTotalCount(finalsql));
		}
		List   stu   =   query.list(); 
		return stu;
	} 
	
	public int getTotalCount(String sql)
	{
		Query   query   = this.getSession().createQuery("select count(*) "+sql); 
		int i=Integer.parseInt(query.list().get(0).toString());
		return i;
	}
	
	public String sumOrder(String sql)
	{
		Query   query   = this.getSession().createQuery("select sum(totalPrice),sum(costTotalPrice) from Order where "+sql); 
		Object[] ob=(Object[])query.list().get(0);
		String tt=ob[0]+"_"+ob[1];
		return tt;
		
		
	}
	//返利数据求和
	public String sumRebackOrder(String sql)
	{
		Query   query   = this.getSession().createQuery("select sum(1), sum(totalPrice) from Order where "+sql); 
		Object[] ob=(Object[])query.list().get(0);
		String tt=ob[0]+"_"+ob[1];
		return tt;
		
		
	}
	
	
	}
